package com.demo.cms.action.user;

import org.apache.commons.lang3.StringUtils;

import com.demo.Constants;
import com.demo.cms.model.Channel;
import com.demo.core.model.Site;
import com.demo.core.model.Template;
import com.demo.webapp.action.TemplateProvider;

/**
 * 频道Action
 * 前端页面展示的Acttion
 * <br>根据频道ID或者首页得到
 * @author ryuu.kk
 *
 */
public class ChannelAction extends ClientBaseAction implements TemplateProvider {

	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 5457908809351588642L;

	/**
	 * 频道模型
	 */
	private Channel channel;
	
	/**
	 * 模板模型
	 */
	private Template template;
	
	@Override
	public String execute() {
		
		// 请求的前期处理
		super.prepare();
		// 频道ID
		Long id = getId();
		// 站点
		Site site = getSite();
		// 频道路径
		String channelPath = getChannelPath();
		if (StringUtils.isNotEmpty(channelPath)) {
			channel = getChannelService().find(channelPath);
		} else {
			if (id == null) {
				// 得到站点默认频道
				channel = getChannelService().findDefault(site.getId());
			} else {
				channel = getChannelService().find(id);
			}
		}
		if (channel == null) {
			// 找不到栏目模型,提示错误
			// 找到错误提示模板,并生成错误提示.
			// 错误提示模板将与其他模板一样,可编辑.
			log.warn("not found this channel by [" + channelPath + "] use default channel.");	
			// do some thing;
			//addActionError(getText("未找到指定的频道."));
			//return 
			return INPUT;
		}
		// 取得渲染模板
		template = findTemplate(channel.getTemplateId());
		return SUCCESS;
	}
	
	// ~~ setter here ============================
	
	public String getActionType() {
		return Constants.ACTION_TYPE_CHANNEL;
	}
	
	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	@Override
	public Template getTemplateModel() {
		return this.template;
	}
}
